package com.java.wysnstudy.lx;

import org.springframework.util.StopWatch;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Test0109 {
    public static void main(String[] args) {
        //String z = "(";
        //
        //String s = "   fly me   to   the moon  ";
        // System.out.println(isValid2("(()){{]]"));
        //isValid(z);
        //lengthOfLastWord(s);
        //System.out.println(numberOfSteps(123));
        //int [][] clips = {{0,1},{6,8},{0,2},{5,6},{0,4},{0,3},{6,7},{1,3},{4,7},{1,4},{2,5},{2,6},{3,4},{4,5},{5,7},{6,9}};
        //int [][] clips = {{8,10},{17,39},{18,19},{8,16},{13,35},{33,39},{11,19},{18,35}};
        //int [][] clips = {{16,18},{16,20},{3,13},{1,18},{0,8},{5,6},{13,17},{3,17},{5,6}};
        //System.out.println(videoStitching(clips,15));

        //int [][] clips2 = {{0,1},{1,2},{3,4}};
        //int [][] clips2 = {{0,2},{4,6},{8,10},{1,9},{1,5},{5,9}};

        //int [][] clips2 = {{0,4},{2,7},{3,7},{4,10},{2,11}};
        //System.out.println(videoStitching(clips2,10));

        //int [][] clips3 = {{24,28},{10,56},{50,78},{38,77},{38,78},{3,69},{33,49},{66,89},{73,83},{6,12},{24,86},{67,82},{18,26},{1,57},{13,30},{8,56},{58,78},{2,84},{35,39},{45,51},{30,32},{19,31},{32,70},{1,15},{16,18},{32,87},{32,87},{39,42},{81,84},{25,61},{26,34},{10,82},{17,34},{56,72},{17,22},{8,83},{5,21},{3,79},{12,73},{0,28},{74,76},{41,79},{4,60},{51,90},{10,41},{47,90},{44,56},{13,16},{43,83},{0,22},{30,40},{8,27},{57,58},{0,26},{16,66},{62,89},{2,74},{17,61},{25,28},{23,54},{42,79},{14,28},{26,77},{34,36},{17,42},{72,81},{12,87},{3,57},{81,88},{65,87},{35,74},{19,77},{10,53},{38,75},{14,90},{10,90},{57,62},{37,74},{24,80},{52,63},{52,55},{64,73},{45,79},{12,19},{26,38},{40,81},{28,48},{33,62},{18,50},{9,40}};
        //System.out.println(videoStitching(clips3,70));

       /*int[] nums = {3,1,3,2,2,1,1,1,2,0,0,4,0,1,0,1,1,1,2,2};
       int[] ints = sortArrayByParityII(nums);
        for (int i = 0; i < ints.length; i++) {
            System.out.println(ints[i]);
        }*/
        //climbStairs(10);
//        int numn1[] = {1,2,2};
//        int numn2[] = {9,6,9,3,15};
//        System.out.println(largestPerimeter(numn1));
        //char s[] = {'a'};
        //char s[] = {'a','b','c'};
        char s[] = {'h','e','l','l','o'};
        //reverseString(s);
        System.out.println(firstUniqChar("loveleetcode"));
    }

    public static  int firstUniqChar(String s) {
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (s.indexOf(chars[i]) == s.lastIndexOf(chars[i]))
                return i;
        }
        return  -1;
    }

    public static void reverseString(char[] s) {
        int length = s.length;
        int i1 = length/2;
        for (int i = 0; i < i1; i++) {
            char c = s[i];
            char c1 = s[length -1 - i];

            s[i] = c1;
            s[length - 1 - i] = c;
        }
        System.out.println(s);
    }

    //   10 10 9 8 7 6 5 4
    public static int largestPerimeter(int[] nums) {
        Arrays.sort(nums);

        int length = 0 ;
        int n = nums.length -1;
        for (int i = n; i > 1; i--) {
            //满足 a + b > c
            int a = nums[i];
            int b = nums[i-1];
            int c = nums[i-2];

            if (a + b > c && a + c > b && c + b > a){
                return a + b + c;
            }
        }
        return length;

    }

    /*  
    给你两个正整数数组nums 和numsDivide。你可以从nums中删除任意数目的元素。
    请你返回使 nums中 最小元素可以整除 numsDivide中所有元素的 最少删除次数。如果无法得到这样的元素，返回 -1。
    如果y % x == 0，那么我们说整数x整除y。
    */
    public static int minOperations(int[] nums, int[] numsDivide) {
        /* 直接暴力解法
        先把nums从小到大排列 然后用nums去整除
        * */
        Arrays.sort(nums);
        for (int i = 0; i < nums.length; i++) {
            int num = nums[i];
            if (i > 1){
                int num1 = nums[i - 1];
                if (num1 == num)
                    continue;
            }

            boolean f = true;
            for (int j = 0; j < numsDivide.length; j++) {
                int i1 = numsDivide[j];
                if (i1 % num != 0){
                    f = false;
                    continue;
                }
            }
            if (f)
                return i;
        }

        return -1;
    }

    // 1 2 3 5
    public static int climbStairs(int n) {
        if (n < 4)
            return n;
        int e = 0;
        int a = 2;
        int c = 3;
        for (int i = 0; i < n - 3; i++) {
            e = a + c;
            a = c;
            c = e;
        }
        return e;
    }

    public static int minMoves(int[] nums) {
        //每次找到最大值
        int n = nums.length;
        int max;
        int zb;
        int cs = 0;
        int cz;
        while (true) {
            max = nums[0];
            zb = 0;
            cz = 0;
            for (int i = 1; i < n; i++) {
                if (nums[i] != max) {
                    if (nums[i] > max) {
                        max = nums[i];
                        zb = i;
                    }
                } else {
                    if (zb == 0)
                        return cs;
                }
            }
            for (int i = 0; i < n; i++) {
                if (i != zb) {
                    int num = nums[i];
                    nums[i] = num + 1;
                }
            }
            cs++;
            //剩下的去加
        }
    }

    public static int[] sortArrayByParityII(int[] nums) {
        int n = nums.length;
        int[] result = new int[n];
        int o = 0;
        int j = 1;
        for (int i = 0; i < n; i++) {
            int num = nums[i];
            if (num % 2 == 0) {
                //偶数
                result[o] = num;
                o = o + 2;
            } else {
                //奇数
                result[j] = num;
                j = j + 2;
            }
        }
        return result;
    }

    public static int videoStitching(int[][] clips, int time) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < clips.length; i++) {
            int[] clip = clips[i];
            //放入相同起来最大的值
            Integer integer = map.get(clip[0]);
            if (integer != null) {
                if (clip[1] > integer) {
                    map.put(clip[0], clip[1]);
                }
            } else {
                map.put(clip[0], clip[1]);
            }
        }
        map.forEach((k, v) -> System.out.println(k + " = " + v));
        Integer csz = map.get(0);
        if (csz == null) {
            return -1;
        } else {
            if (csz >= time)
                return 1;
        }
        //现在得到了从0开始的初始值 要从 csz 往 0 去找 找出最大的值
        int start = 0;
        int cs = 1;
        while (true) {
            int temp = -1;
            int bz = 0;
            for (int i = csz; i > start; i--) {
                Integer integer = map.get(i);
                if (integer != null) {
                    if (integer >= time)
                        return ++cs;
                    if (integer > temp) {
                        temp = integer;
                    }
                    if (integer == temp) {
                        bz = i;
                    }
                }
            }
            //如果没有找到值就表示断了
            if (temp == -1)
                return -1;
            start = bz;
            csz = temp;
            cs++;
        }

    }

    public static int numberOfSteps(int num) {
        int count = 0;
        while (num != 0) {
            if (num % 2 == 0) {
                num = num / 2;
            } else {
                --num;
            }
            count++;
        }
        return count;
    }

    public static int lengthOfLastWord(String s) {
        String[] s1 = s.split(" ");
        return s1[s1.length - 1].length();
    }

    public static boolean isValid(String s) {
        int length = s.length();
        if (length > 0 && length % 2 == 0) {
            String z = "({[";
            String y = ")}]";
            char[] chars = s.toCharArray();
            String zc = "";
            String yc = "";
            String pj = "";
            for (int i = 0; i < length; i++) {
                System.out.println("初始i=>" + i);
                String aChar = chars[i] + "";
                //先判断是左还是右
                if (z.contains(aChar)) {
                    zc += aChar;
                    if (aChar.equals("(")) {
                        yc = ")" + yc;
                    } else if (aChar.equals("{")) {
                        yc = "}" + yc;
                    } else {
                        yc = "]" + yc;
                    }
                    //防止出现 （（（（（（（（（（ {{{[[ 这种情况
                    if (zc.length() + yc.length() + pj.length() > length)
                        return false;
                } else {
                    //防止 ））））}}}}]]]]
                    if (zc.length() == 0)
                        return false;
                    //进到右边
                    pj = pj + zc + yc;
                    //System.out.println(pj);
                    if (pj.length() == length) {
                        return pj.equals(s);
                    }
                    //System.out.println("i=>"+i);
                    //System.out.println(s.substring(0,pj.length()));
                    if (s.substring(0, pj.length()).equals(pj)) {
                        i = pj.length() - 1;
                        zc = "";
                        yc = "";
                    } else {
                        return false;
                    }

                }
                //System.out.println("zc=>"+zc);
                //System.out.println("yc=>"+yc);
            }
        }

        return false;
    }

    public static boolean isValid2(String s) {
        int length = s.length();
        if (length > 0 && length % 2 == 0) {
            String z = "({[";
            char[] chars = s.toCharArray();
            String zc = "";
            for (int i = 0; i < length; i++) {
                String aChar = chars[i] + "";
                //先判断是左还是右
                if (z.contains(aChar)) {
                    zc += aChar;
                } else {
                    if (zc.length() == 0)
                        return false;
                    //跟消消乐一样 去消除
                    if (aChar.equals(")")) {
                        if (!zc.endsWith("(")) {
                            return false;
                        }
                    }
                    if (aChar.equals("}")) {
                        if (!zc.endsWith("{")) {
                            return false;
                        }
                    }
                    if (aChar.equals("]")) {
                        if (!zc.endsWith("[")) {
                            return false;
                        }
                    }
                    zc = zc.substring(0, zc.length() - 1);
                }
            }
            //判断左边得字符串是不是0
            return zc.length() == 0;
        }

        return false;
    }


}
